<?php
include_once("../commonFunctions.php");
class TT {

    var $id;
    var $market;
    var $storeAbbrev;
    var $tableName;
    var $tableID;
    var $type; /////////////
    var $description; //////
    var $submittedBy;
    var $submittedByName;
    var $assignedTo;
    var $dateTimeSubmitted;
    var $dateTimeFinished;
    var $specialID;
    var $statusArr = array();
    var $currentStatus;

    function __construct($ttID) {
        $query = "select * from tt where id='" . $ttID . "';";
        $result = mysql_query($query) or die("query failed in tt.construct.php");
        if (mysql_num_rows($result) > 0) {
            $row = mysql_fetch_array($result, MYSQL_ASSOC);
            $this->id = $row['id'];
            $this->market = $row['market'];
            $this->storeAbbrev = $row['storeAbbrev'];
            $this->tableName = $row['tableName'];
            $this->tableID = $row['tableID'];
            $this->submittedBy = $row['submittedBy'];
            $subDealer = new Dealer($this->submittedBy);
            $this->submittedByName = $subDealer->firstName . ' ' . $subDealer->lastName;
            $this->assignedTo = $row['assignedTo'];
            $this->dateTimeSubmitted = $row['dateTimeSubmitted'];
            $this->dateTimeFinished = $row['dateTimeFinished'];
            $this->specialID = $row['specialID'];
            $this->currentStatus = $row['currentStatus'];
            $query = 'select *
                    from ttstatuschange where
                    id = ANY
                        (select TTStatusID
                        from ttstatus1m
                        where TTID = ANY
                                (select id
                                from tt
                                where id = ' . $ttID . '));';
            $result = mysql_query($query) or die("Query failed in TT.fetchTTS.php<br/>" . $query);
            while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
                array_push($this->statusArr, (new TTStatusChange($row['id'])));
            }
  
            $query = 'select * from ' . strtolower($this->tableName) . ' where id = ' . strtolower($this->tableID) . ';';
            $result = mysql_query($query) or die("Query failed while getting type and description in TT.php");
            $row = mysql_fetch_array($result, MYSQL_ASSOC);
            $this->type = $row['type'];
            $this->description = $row['description'];
        } else {
            return null;    // TTID doens't exist
        }
    }

    function fetchUnresolvedTT($storeAbbrev, $tableName) {
        // Retrieves the ttid of all
        $query = "select id from tt where storeAbbrev='" . $storeAbbrev . "' AND tableName='" . $tableName . "';";
        $result = mysql_query($query) or die("query failed in TT.fetchUnresolvedTT.php</br>" . $query);
        $tts = array();
        for ($i = 0; $i < mysql_num_rows($result); $i++) {
            $row = mysql_fetch_array($result, MYSQL_ASSOC);
            $add = true;
            $tt = new TT($row['id']);
            for ($j = 0; $j < count($tt->statusArr); $j++) {
                if ($tt->statusArr[$j]->status == 'Resolved') {
                    $add = false;
                    break;
                }
            }
            if ($add) {
                array_push($tts, $tt);
            }
        }
        return $tts;
    }



}

?>
